package cn.iocoder.yudao.module.shop.staff.mq;

import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.module.api.member.user.message.MemberUserDeleteMessage;
import cn.iocoder.yudao.module.api.shop.info.message.ShopAuditPassMessage;
import cn.iocoder.yudao.module.api.shop.info.message.ShopInfoUpdateMessage;
import cn.iocoder.yudao.module.api.shop.staff.dto.ShopStaffSaveDTO;
import cn.iocoder.yudao.module.api.trade.scan.message.ScanOrderPaidMessage;
import cn.iocoder.yudao.module.shop.staff.dao.ShopStaffMapper;
import cn.iocoder.yudao.module.shop.staff.service.ShopStaffService;
import cn.iocoder.yudao.module.shop.staff.ws.ScanOrderWebSocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 店员 事件消费者
 *
 * @author 山野羡民
 */
@Component
@Slf4j
public class ShopStaffConsumer {

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(MemberUserDeleteMessage message) {
        log.info("[shop/staff][onMessage][用户删除事件({})]", message);
        SpringUtil.getBean(ShopStaffMapper.class).deleteByUserId(message.getUserId(), UserTypeEnum.MEMBER.getValue());
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(ShopAuditPassMessage message) {
        log.info("[shop/staff][onMessage][店铺审核通过事件({})]", message);
        ShopStaffSaveDTO reqVO = new ShopStaffSaveDTO();
        reqVO.setContactName(message.getShopInfo().getContactName());
        reqVO.setContactMobile(message.getShopInfo().getContactMobile());
        SpringUtil.getBean(ShopStaffService.class).saveStaff(reqVO);
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(ShopInfoUpdateMessage message) {
        log.info("[shop/staff][onMessage][店铺资料更新事件({})]", message);
        ShopStaffSaveDTO reqVO = new ShopStaffSaveDTO();
        reqVO.setContactName(message.getShopInfo().getContactName());
        reqVO.setContactMobile(message.getShopInfo().getContactMobile());
        SpringUtil.getBean(ShopStaffService.class).saveStaff(reqVO);
    }

    @EventListener
    @Async // Spring Event 默认在 Producer 发送的线程，通过 @Async 实现异步
    public void onMessage(ScanOrderPaidMessage message) {
        log.info("[shop/staff][onMessage][扫码订单付款完成事件({})]", message.getOrder().getTradeNo());
        SpringUtil.getBean(ScanOrderWebSocket.class).sendScanOrderPaidMessage(message);
    }

}
